package com.libertyville.adapter.driven.persistence.orgmng;

import com.libertyville.domain.orgmng.OrgType;
import com.libertyville.domain.orgmng.OrgTypeRepository;
import com.libertyville.domain.orgmng.OrgTypeStatus;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;

@Repository
public class OrgTypeRepositoryImpl implements OrgTypeRepository {
    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public boolean existsByIdAndOrgTypeCodeAndStatus(Integer tenantId, String orgTypeCode, OrgTypeStatus orgTypeStatus) {
        List<OrgType> results = entityManager.createQuery("from OrgType ot where ot.tenantId = :tenantId and ot.orgTypeCode = :orgTypeCode and ot.orgTypeStatus = :status", OrgType.class)
                .setParameter("tenantId", tenantId)
                .setParameter("orgTypeCode", orgTypeCode)
                .setParameter("status", orgTypeStatus)
                .getResultList();
        return results.size() == 1;
    }
}
